


<div id='GoodsCon_List' class='GoodsCon_Item' >
	<h2 style='text-align:center'>商品服务管理</h2>
	<div style='margin:5px 0;display:flex;height:26px;border:0px solid'>
		<div style='flex:1'>
			<input id='GoodsCon_SearchInp' type="text" style='border-radius:5px;width:100%;height:100%' />
		</div>
		<div id='GoodsCon_SearchBtn' style='border-radius:5px;width:40px;height:100%;box-sizing:border-box;padding:3px 10px;background:#eee'><img src='../Public/Image/search.png' style='height:20px' alt="" /></div>
	</div>

	<div>
		<select style='width:100%' id="GoodsCon_SelType" >
			<option value="6">全部</option>
			<option value="1">审核中</option>
			<option value="2">正常</option>
			<option value="3">未审过</option>
			<option value="4">已封禁</option>
			<option value="5">已下架</option>
		</select>
	</div>
	
	
	<div id='GoodsCon_ListArea'></div> 	<!--商品列表区-->

	<div id='GoodsCon_FpageArea'></div>	<!--分页区-->

</div>

<div id='GoodsCon_Add' class='GoodsCon_Item' style='display:none'>2</div><!--商品添加区-->

<div id='GoodsCon_Edit' class='GoodsCon_Item' style='display:none'>2</div><!--商品编辑区-->

<div style='text-align:center;margin:20px auto'>
	<button id='GoodsCon_AddBtn' onclick=GoodsCon_Add()>添加商品或服务</button>
</div>




<script>
var pnow = 1;
var psize = 3;

GoodsCon_Init();

//点击搜索
$('#GoodsCon_SearchBtn').click(function(){
	pnow = 1;
	GoodsCon_Init();
});

//商品状态切换
$('#GoodsCon_SelType').change(function(){
	pnow = 1;
	GoodsCon_Init();
});


function GoodsCon_Init(){
	GoodsCon_GetGoodsList(function(res){		//获取商品列表
		GoodsCon_GetGoodsListParse(res);		//解析列表
	});
}




//获取商品列表
function GoodsCon_GetGoodsList(fun){
	var data = {
		type:'GetGoodsList',
		store_id:StoreInfo.id,
	};

	Ajax2(data,function(res){
		fun(res);
	},after_url);

}


//解析列表
function GoodsCon_GetGoodsListParse(res){
	$('#GoodsCon_ListArea').html('');
	if(/^\[{"id":.*$/.test(res)){
		var sel_type = parseInt($('#GoodsCon_SelType').find('option:selected').val());
		var search_val = $('#GoodsCon_SearchInp').val().trim();
		var info1 = JSON.parse(res);

		var info = new Array();
		if(sel_type  == 6){
			if(search_val){
				for(var i=0;i<info1.length;i++){
					if(!info1[i].title.match(search_val)){
						continue;
					}else{
						info.push(info1[i]);
					}
				}
			}else{
				info = info1;
			}
		}else{
			for(var i=0;i<info1.length;i++){
				if(sel_type == info1[i].goods_stat){
					if(search_val && !info1[i].title.match(search_val)){
						continue;
					}else{
						info.push(info1[i]);
					}
				}
			}
		}
		var start = (pnow-1) * psize;
		var end = start + psize;
		if(end > info.length){
			end = info.length;
		}
		var pnum = Math.ceil(info.length/psize);

		var str = '';
		for(var i=start;i<end;i++){
			//根据id获取商品信息
			var goods_id = info[i].id;
			str += "<div style='display:flex;border-bottom:7px solid #fee;font-size:13px;margin:15px 0;'>";
			str += "<div style='border:1px solid #eef;width:100px;height:100px'>";
			//商品封面
			if(info[i].banner){
				var img_addr = upyun_shop_img+ChangeChar(info[i].banner,1);
			}else{
				var img_addr = base_img;
			}
			str += "<img src='"+img_addr+"' style='width:100%;height:100%' onerror=this.src='"+base_img+"' alt='呵呵了' ></div>";
			str += "<div style='flex:1;padding:0 5px;box-sizing:border-box;color:#999'>";
			//标题
			str += "<div style='font-size:14px;height:40px;word-break:break-all;overflow:hidden;color:#333'>"+ChangeChar(info[i].title,2)+"</div>";		
			str += "<div style='height:20px;display:flex;margin:8px 0'>";
			//价格
			str += "<div style='flex:1;color:red;font-weight:bold'>￥："+info[i].price+"</div>";
			//编号
			str += "<div style='flex:1;text-align:right'>编号："+info[i].number+"</div></div>";
			str += "<div style='height:20px;display:flex'>";
			//状态
			switch(info[i].goods_stat){
				case 2:case '2':var stat_str = '<text style="color:blue">正常</text>';break;
				case 3:case '3':var stat_str = '审核未过';break;
				case 4:case '4':var stat_str = '已封禁';break;
				case 5:case '5':var stat_str = '已下架';break;
				default :
					var stat_str = '审核中';break;
			}
			str += "<div style='width:60px'>"+stat_str+"</div>";

			str += "<div style='flex:1;text-align:right'>";
			//封禁的商品不可操作
			if(info[i].goods_stat != 4){
				if(info[i].goods_stat == 2 || info[i].goods_stat == 5){  //2为上架，5为下架
					var text = info[i].goods_stat== 2?'下架':'上架';
					var text_type = info[i].goods_stat== 2?5:2;
					str += "<span style='padding:1px 7px;background:#5ee;color:#fff;border-radius:5px' onclick=GoodsCon_ChangeGoodsStat("+goods_id+","+text_type+")>"+text+"</span>　";
				}
				//if(parseInt(info[i].free_id) < 1){
					str += "<span style='padding:1px 7px;background:#aaf;color:#fff;border-radius:5px' onclick='GoodsCon_Edit("+goods_id+")'>编辑</span>　";
				//}
				//if(parseInt(info[i].free_id) == 0){
					str += "<span style='padding:1px 7px;background:#aaa;color:#fff;border-radius:5px' onclick='GoodsCon_Del("+goods_id+")'>删除</span>";
				//}
			}
			str += "</div></div></div></div>";
		}

		$('#GoodsCon_ListArea').html(str);

		var max = pnow+1 < pnum?pnow+1:pnum;
		Fpage(pnow,max,function(p){
			pnow = p;
			GoodsCon_Init();
		},1,1,'#GoodsCon_FpageArea');
	}else{
		if(pnow <=1){
			$('#GoodsCon_FpageArea').html('');
		}
	}
}


	//商品的上架下架
	function GoodsCon_ChangeGoodsStat(id,stat){
		var data = {
			type:'ChangeGoodsStat',
			id:id,
			stat:stat
		};
		Ajax2(data,function(res){
			if(res=='ok'){
				A('操作成功！');
				GoodsCon_Init();
			}else{
				A('操作失败！');
			}
		},after_url);
	}


	//商品的删除 
	function GoodsCon_Del(id){
		CheckControl();
		Notice('删除中......');
		var data = {
			type:'StoreGoodsDel',
			id:id
		};
		Ajax2(data,function(res){
			C(res);
			Notice('');
			if(res=='ok'){
				A('操作成功！');
				GoodsCon_Init();
			}else{
				A('操作异常！');
			}
		},after_url);
	}


	//添加商品
	function GoodsCon_Add(id){
		$('.GoodsCon_Item').hide();
		$('#GoodsCon_AddBtn').hide();
		Ajax('',function(res){
			$('#GoodsCon_Add').html(res).show();
		},'Template/GoodsConAdd.html');
	}


	//编辑商品
	function GoodsCon_Edit(id){
		sessionStorage.goods_edit_id = id;
		$('.GoodsCon_Item').hide();
		$('#GoodsCon_AddBtn').hide();
		Ajax('',function(res){
			$('#GoodsCon_Edit').html(res).show();
		},'Template/GoodsConEdit.html');
	}
</script>